/**
 * 
 */
package org.xtense.rcp.editors.action;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.actions.ActionGroup;
import org.xtense.data.DBConfig;
import org.xtense.data.DBManager;
import org.xtense.engine.actions.DeleteAction;
import org.xtense.engine.actions.QueryAction;
import org.xtense.engine.actions.XAction;
import org.xtense.rcp.editors.DatabaseViewer;
import org.xtense.rcp.editors.XmlEditor;
import org.xtense.rcp.editors.input.XmlEditorInput;
import org.xtense.rcp.editors.util.TableEntry;
import org.xtense.rcp.util.DocTablelUtil;
import org.xtense.rcp.util.ImagesContext;
import org.xtense.rcp.views.QueryView;

/**
 * @author Owner
 *
 */
public class DatabaseEditorActionGroup extends ActionGroup {
	private DatabaseViewer databaseViewer;
	private TableViewer tv;
	private MenuManager menuManager;

	/**
	 * @param tv
	 */
	public DatabaseEditorActionGroup(DatabaseViewer dbv, TableViewer tv) {
		this.databaseViewer = dbv;
		this.tv = tv;

		menuManager = new MenuManager();

		menuManager.add(new RefreshAction());
		menuManager.add(new OpenAction());
		menuManager.add(new RemoveAction());
	}

	public void fillContextMenu() {
		Table table = tv.getTable();
		Menu menu = menuManager.createContextMenu(table);

		table.setMenu(menu);
	}

	private class RefreshAction extends Action {
		public RefreshAction() {
			setText("Refresh");
			setImageDescriptor(ImagesContext
					.getImageDescriptor(ImagesContext.Refresh));
		}

		public void run() {
			DocTablelUtil.createDocTable(tv);
		}
	}

	private class OpenAction extends Action {
		public OpenAction() {
			setText("Open");
			setImageDescriptor(ImagesContext
					.getImageDescriptor(ImagesContext.OpenDB));
		}

		public void run() {
			for (TableItem item : tv.getTable().getSelection()) {
				TableEntry entry;
				if (item.getData() instanceof TableEntry) {
					entry = (TableEntry) item.getData();
					// open the xml editor
					IWorkbenchPage workbenchPage = databaseViewer.getEditorSite()
							.getPage();
					IEditorInput input = new XmlEditorInput();
					IEditorPart editor = workbenchPage.findEditor(input);
					if (editor != null) {
						workbenchPage.bringToTop(editor);
					} else {
						try {
							editor = workbenchPage.openEditor(input,
									XmlEditor.class.getName());
						} catch (PartInitException ex) {
							ex.printStackTrace();
						}
					}
					// query for the whole document
					XAction query = new QueryAction(new DBManager(DBConfig
							.getInstance(false, null)), entry.getId());

					((XmlEditor) editor).updateToolBar(false, entry.getId());
					((XmlEditor) editor).showResult(query.execute("/"));
					((XmlEditor) editor).setPartName(entry.getName());

				}
			}
		}
	}

	private class RemoveAction extends Action {
		public RemoveAction() {
			setText("Remove");
			setImageDescriptor(ImagesContext
					.getImageDescriptor(ImagesContext.Delete));
		}

		public void run() {
			Table table = tv.getTable();
			for (TableItem item : table.getSelection()) {
				TableEntry entry;
				if (item.getData() instanceof TableEntry) {
					entry = (TableEntry) item.getData();
					XAction delete = new DeleteAction(new DBManager(DBConfig
							.getInstance(false, null)), entry.getId());
					delete.execute(null);
					QueryView.removeDoc(entry.getName());
				}
			}
			table.remove(table.getSelectionIndices());
		}
	}

}
